/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;
import org.kxml2.io.KXmlParser;
import org.xmlpull.v1.*;

/**
 *
 * @author psychok7
 */
public class ReadXML extends Thread {

    private Vector classesVector = new Vector();
    private String xmlstr, mode, info, type;

    public ReadXML(String xmlstr, String mode, String type) {
        this.xmlstr = xmlstr;
        this.mode = mode;
        this.type = type;
    }

    private void readXMLData(KXmlParser parser) throws IOException, XmlPullParserException {

        if (mode.equalsIgnoreCase("getAll")) {
            //Parse our XML file
            parser.require(XmlPullParser.START_TAG, null, "classes");
            Classes data = new Classes();
            while (parser.nextTag() != XmlPullParser.END_TAG) {
                parser.require(XmlPullParser.START_TAG, null, null);
                String name = parser.getName();
                String text = parser.nextText();

                if (name.equals("nome")) {
                    data.setNome(text);
                } else if (name.equals("regime")) {
                    data.setRegime(text);
                } else if (name.equals("ects")) {
                    data.setEcts(text);
                } else if (name.equals("codigo")) {
                    data.setCodigo(text);
                } else if (name.equals("horario")) {
                    data.setHorario(text);
                } else if (name.equals("tipo")) {
                    data.setTipo(text);
                } else if (name.equals("horario_dia")) {
                    data.setHorario_dia(text);
                }

                parser.require(XmlPullParser.END_TAG, null, name);
            }
            System.out.println("tipo: " + this.type);
            if (data.getNome().equalsIgnoreCase(this.type)) {
                this.info = this.info + data.getNome() + "," + data.getRegime() + "," + data.getEcts() + "," + data.getCodigo() + "," + data.getHorario() + "," + data.getTipo() + "," + data.getHorario_dia();

            }
            parser.require(XmlPullParser.END_TAG, null, "classes");

        } else if (mode.equalsIgnoreCase("getCadeiras")) {
            //Parse our XML file
            parser.require(XmlPullParser.START_TAG, null, "classes");
            Classes data = new Classes();
            while (parser.nextTag() != XmlPullParser.END_TAG) {
                parser.require(XmlPullParser.START_TAG, null, null);
                String name = parser.getName();
                String text = parser.nextText();

                if (name.equals("nome")) {
                    data.setNome(text);
                }

                parser.require(XmlPullParser.END_TAG, null, name);
            }
            this.info = this.info + "," + data.getNome();

            //this.getClassesVector().addElement(data);
            parser.require(XmlPullParser.END_TAG, null, "classes");

        } else if (mode.equalsIgnoreCase("getHorarios")) {
            //Parse our XML file
            parser.require(XmlPullParser.START_TAG, null, "classes");
            Classes data = new Classes();
            while (parser.nextTag() != XmlPullParser.END_TAG) {
                parser.require(XmlPullParser.START_TAG, null, null);
                String name = parser.getName();
                String text = parser.nextText();

                if (name.equals("nome")) {
                    data.setNome(text);
                } else if (name.equals("horario")) {
                    data.setHorario(text);
                } else if (name.equals("horario_dia")) {
                    data.setHorario_dia(text);
                }

                parser.require(XmlPullParser.END_TAG, null, name);
            }

            if (this.type.equalsIgnoreCase(data.getHorario_dia())) {
                this.info = this.info + data.getHorario() + "," + data.getNome() + "," + data.getHorario_dia() + ";";
            }

            //this.getClassesVector().addElement(data);
            parser.require(XmlPullParser.END_TAG, null, "classes");
        }
        else if (mode.equalsIgnoreCase("getMaterial")) {
            //Parse our XML file
            parser.require(XmlPullParser.START_TAG, null, "classes");
            Classes data = new Classes();
            while (parser.nextTag() != XmlPullParser.END_TAG) {
                parser.require(XmlPullParser.START_TAG, null, null);
                String name = parser.getName();
                String text = parser.nextText();

                if (name.equals("nome")) {
                    data.setNome(text);
                }

                parser.require(XmlPullParser.END_TAG, null, name);
            }

            
                this.info = this.info + data.getNome() ;
            

            //this.getClassesVector().addElement(data);
            parser.require(XmlPullParser.END_TAG, null, "classes");
        }


    }

    public void run() {
        try {
            //Open http connection
            //HttpConnection httpConnection = (HttpConnection) Connector.open("http://localhost:8080/examples/classes.xml");

            //Initilialize XML parser
            KXmlParser parser = new KXmlParser();
            byte[] xmlByteArray = xmlstr.getBytes();
            ByteArrayInputStream xmlStream = new ByteArrayInputStream(xmlByteArray);
            parser.setInput(new InputStreamReader(xmlStream));
            parser.nextTag();
            parser.require(XmlPullParser.START_TAG, null, "class");

            System.out.println("modo: " + mode);
            clearInfo();
            //Iterate through our XML file
            while (parser.nextTag() != XmlPullParser.END_TAG) {
                readXMLData(parser);
            }
            parser.require(XmlPullParser.END_TAG, null, "class");
            parser.next();
            parser.require(XmlPullParser.END_DOCUMENT, null, null);
            // for (int i=0;i<classesVector.size();i++)            
            //   classesVector.elementAt(i

        } catch (Exception e) {
            e.printStackTrace();
            //resultItem.setLabel ("Error:");
            //resultItem.setText (e.toString ());

        }
    }

    /**
     * @return the classesVector
     */
    public Vector getClassesVector() {
        return classesVector;
    }

    /**
     * @param classesVector the classesVector to set
     */
    public void setClassesVector(Vector classesVector) {
        this.classesVector = classesVector;
    }

    public String getInfo() {
        return this.info;
    }

    public void clearInfo() {
        this.info = "";
    }
}
